package com.tdam2011.grupo03.activitys;

import java.text.ParseException;
import java.util.ArrayList;
import com.tdam2011.grupo03.R;
import com.tdam2011.grupo03.dbutils.DatabaseHelper;
import com.tdam2011.grupo03.entities.ContactoAlertasFechas;
import android.app.ListActivity;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AlertasFechas extends ListActivity {
	private AlertaAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_alertas);
		adapter = new AlertaAdapter();
		setListAdapter(adapter);
		loadAlertas();
	}

	private void loadAlertas() {
		DatabaseHelper dbHelper = new DatabaseHelper(this);
		int cantDias = Integer.parseInt(PreferenceManager
				.getDefaultSharedPreferences(this)
				.getString("alertConfig", "1"));
		ContactoAlertasFechas[] alertas;
		try {
			alertas = dbHelper.getAlertasFechas(cantDias);

			if (alertas != null) {
				for (int i = 0; i <= (alertas.length - 1); i++) {
					adapter.addAlerta(alertas[i]);
				}

				adapter.notifyDataSetChanged();
			} else {
				Toast toast = Toast.makeText(AlertasFechas.this, getResources()
						.getString(R.string.msgNoAlertsToShow),
						Toast.LENGTH_LONG);
				toast.show();
			}

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast toast = Toast.makeText(AlertasFechas.this, e.toString(),
					Toast.LENGTH_LONG);
			toast.show();
		}

	}

	class AlertaAdapter extends BaseAdapter {
		private ArrayList<ContactoAlertasFechas> AlertasInfos;
		private LayoutInflater inflater;

		public AlertaAdapter() {
			AlertasInfos = new ArrayList<ContactoAlertasFechas>();
			inflater = LayoutInflater.from(AlertasFechas.this);
		}

		public void addAlerta(ContactoAlertasFechas info) {
			if (info != null) {
				AlertasInfos.add(info);
			}
		}

		@Override
		public int getCount() {
			return AlertasInfos.size();
		}

		@Override
		public Object getItem(int position) {
			return AlertasInfos.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		class Holder {
			private ImageView foto;
			private TextView txtContacto;
			private TextView txtCumpleanos;
			private TextView txtAniversario;
			private TextView txtOtro;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			Holder holder;

			if (convertView == null) {
				convertView = inflater.inflate(R.layout.lista_alertas_item,
						null);
				holder = new Holder();
				holder.foto = (ImageView) convertView.findViewById(R.id.idFoto);
				holder.txtContacto = (TextView) convertView
						.findViewById(R.id.txtContacto);
				holder.txtCumpleanos = (TextView) convertView
						.findViewById(R.id.txtCumpleanos);
				holder.txtAniversario = (TextView) convertView
						.findViewById(R.id.txtAniversario);
				holder.txtOtro = (TextView) convertView
						.findViewById(R.id.txtOtro);
				convertView.setTag(holder);
			}

			else {
				holder = (Holder) convertView.getTag();
			}

			ContactoAlertasFechas AlertasInfo = (ContactoAlertasFechas) getItem(position);
			holder.foto.setImageURI(AlertasInfo.getFoto());
			holder.txtContacto.setText(AlertasInfo.getName());

			if (!AlertasInfo.getFechaUno().isEmpty()) {
				holder.txtCumpleanos.setText(getResources().getString(
						R.string.tvBirthday)
						+ ": " + AlertasInfo.getFechaUno());
			}

			if (!AlertasInfo.getFechaDos().isEmpty()) {
				holder.txtAniversario.setText(getResources().getString(
						R.string.tvAnniversary)
						+ ": " + AlertasInfo.getFechaDos());
			}

			if (!AlertasInfo.getFechaTres().isEmpty()) {
				holder.txtOtro.setText(getResources().getString(
						R.string.tvOther)
						+ ": " + AlertasInfo.getFechaTres());
			}

			return convertView;
		}

	}

}
